package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"user-center/src/common"
	"user-center/src/model/request"
	"user-center/src/model/response"
	"user-center/src/service"
)


// FriendAdd
// @Tags        friend
// @Summary     添加好友
// @Description  添加好友
// @Param data body request.FriendAddRequest true "参数描述"
// @Router /friend/add [post]
func FriendAdd(ctx *gin.Context) {
	req := &request.FriendAddRequest{}
	err := parsePostJsonRequest(ctx, req)
	if err != nil {
		return
	}
	//处理请求
	currentUser := service.GetUserCurrentInfo(ctx)
	res := service.FriendAdd(ctx, req.FriendId, currentUser)
	ctx.JSON(http.StatusOK, res)
}

// FriendDelete
// @Tags        friend
// @Summary     删除好友
// @Description  删除好友
// @Param data body request.FriendAddRequest true "参数描述"
// @Router /friend/delete [post]
func FriendDelete(ctx *gin.Context) {
	req := &request.FriendAddRequest{}
	err := parsePostJsonRequest(ctx, req)
	if err != nil {
		return
	}
	//处理请求
	currentUser := service.GetUserCurrentInfo(ctx)
	res := service.FriendDelete(ctx, req.FriendId, currentUser)
	ctx.JSON(http.StatusOK, res)
}

// FriendChatAdd
// @Tags        friend
// @Summary     好友发送消息
// @Description  好友发送消息
// @Param data body request.FriendChatAddRequest true "参数描述"
// @Router /friend/chat/sendMessage [post]
func FriendChatAdd(ctx *gin.Context) {
	req := &request.FriendChatAddRequest{}
	err := parsePostJsonRequest(ctx, req)
	if err != nil {
		return
	}
	//处理请求
	currentUser := service.GetUserCurrentInfo(ctx)
	res := service.FriendChatAdd(ctx, req, currentUser)
	ctx.JSON(http.StatusOK, res)
}

// FriendChatList FriendLists GET
// @Tags        friend
// @Summary     所有好友的最新消息列表
// @Description  所有好友的最新消息列表
// @Param pageNum query int true "页数"
// @Param pageSize query int true "每页大小"
// @Router /friend/chat/list [get]
func FriendChatList(ctx *gin.Context) {
	pageNum, pageSize := pageNumAndSize(ctx)
	currentUser := service.GetUserCurrentInfo(ctx)
	ctx.JSON(http.StatusOK, service.ListFriends(ctx, pageNum, pageSize, currentUser))
}

// FriendChatHistory  GET
// @Tags        friend
// @Summary     好友的历史消息
// @Description  好友的历史消息
// @Param pageNum query int true "页数"
// @Param pageSize query int true "每页大小"
// @Router /friend/chat/history [get]
func FriendChatHistory(ctx *gin.Context) {
	res := &response.BaseResponse{}
	pageNum, pageSize := pageNumAndSize(ctx)
	friendId, err := strconv.ParseInt(ctx.Query("friendId"), 10, 64)

	if err != nil {
		res.ResponseWithError(common.PARAMS_ERROR, "参数错误")
		ctx.JSON(http.StatusOK, res)
		return
	}
	ctx.JSON(http.StatusOK, service.ListHistoryByFriend(ctx, pageNum, pageSize, friendId))
}
